package action.adapter;

import java.util.ArrayList;

import action.newelifile.LocalActivity;
import action.newelifile.R;
import action.pojo.LocalFilePojo;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LocalFileAdapter extends BaseAdapter{
	private Context context;
	ArrayList<LocalFilePojo> local_list;
	ViewHolder holder;
	int layoutid;
	
	public LocalFileAdapter(LocalActivity context,int textViewResourceId,ArrayList<LocalFilePojo> local_list){
		this.context=context;
		this.local_list=local_list;
		this.layoutid=textViewResourceId;
		
	}
	public LocalFileAdapter(Context context,int textViewResourceId,ArrayList<LocalFilePojo> local_list){
		this.context=context;
		this.local_list=local_list;
		this.layoutid=textViewResourceId;
		
	}
	@Override
	public int getCount() {
		return local_list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return local_list.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final LocalFilePojo f = local_list.get(position);
		Drawable drawable;
		if(convertView==null){
			holder = new ViewHolder();
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView=layoutInflater.inflate(layoutid, parent,false);
			holder.txtname = (TextView) convertView.findViewById(R.id.name);
			holder.txtinfo = (TextView)convertView.findViewById(R.id.info);
			holder.imiIcon = (ImageView) convertView.findViewById(R.id.icon);
			holder.imiCheck =(ImageView)convertView.findViewById(R.id.checked);
			holder.imiFavorite = (ImageView)convertView.findViewById(R.id.favorite);
//			convertView.setBackgroundResource(R.drawable.list_selector);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		if(f.getFileItem()!=null){
			if(!f.getFileItem().getName().equals("")){
				holder.txtname.setText(f.getFileItem().getName());
			}
			if(holder.txtinfo !=null){
				holder.txtinfo.setText(f.getInfodetail(context.getString(R.string.folder),context.getString(R.string.file)));
			}
			if(f.isChecked()){
				((ImageView)convertView.findViewById(R.id.checked)).setVisibility(ImageView.VISIBLE); 
			}else{
				((ImageView)convertView.findViewById(R.id.checked)).setVisibility(ImageView.GONE); 
			}
			if(!f.getFileItem().getName().equals("")){
				drawable = f.getIcon();
				holder.imiIcon.setImageDrawable(drawable);
			}
			if(f.isFavorite()){
				holder.imiFavorite.setVisibility(ImageView.VISIBLE); 
			}else{
				holder.imiFavorite.setVisibility(ImageView.GONE); 
			}
		}
		
		return convertView;
	}
	
	public static class ViewHolder {
		TextView txtname,txtinfo;
		ImageView imiIcon,imiCheck,imiFavorite;
    }
	
}
